define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, Unsalableused, GoAddress,GoDialog, $window, $timeout, $ionicSideMenuDelegate, GoPage, GoCatTypeAuth, $ionicScrollDelegate) {
		//商品类型:二手产品为10，租赁产品为9
		$scope.pageType = 9;
		$scope.initialization = function () {
			$scope.datas = new Object();
			//默认排序为价格从高到低:4。价格从低到高:5
			$scope.input = new Object();
			$scope.input.nPage = 1;
			$scope.input.key = '';
			$scope.orderby = 1;
			//地区格式为字符串，形如：region_A_01,region_B_01,region_C_01
			$scope.region = '';
			//品牌格式为字符串，形如：store_cat_A_01,store_cat_B_01,store_cat_C_01
			$scope.store_cat = '';
			$scope.act_id = '';
			$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.count = null;
		}
		$scope.initialization();//初始化变量

		//画面加载
		$scope.$on('$ionicView.afterEnter', function () {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if (window.cordova && window.cordova.plugins) {
				if (ionic.Platform.isIOS()) {
					lan = 20;
				}
			}
			var all = $window.innerHeight;
			var orderBar = document.getElementById("orderBar").clientHeight;
			$scope.myObj = {
				"height": all - 44 - 44 - lan - orderBar + "px"
			};
			$scope.init_data();//初始化数据
		}, false)

		//滑动切换
		$scope.selectTab = 0;
		$scope.activeTab = function (index) {
			if (index != $scope.selectTab) {//切换页面，才去重新请求
				$scope.isLoad = true;//如果不加这个，$ionicScrollDelegate.scrollTop()会请求到上拉刷新，这样就会请求2次
				$ionicScrollDelegate.scrollTop();
				$scope.initialization();//初始化变量
				if (index == 0) {//如果为第一个tab，则是"二手"产品
					$scope.pageType = 9;
				} else {
					$scope.pageType = 10;
				}
				$scope.init_data();//初始化数据
			}
			$scope.selectTab = index;
			if ($ionicSideMenuDelegate.isOpenRight()) {//关闭侧边菜单
				$scope.rightSideMenu();
			}
		};
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function (data) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.district_items = null;
		};
		$scope.listen_city = function (data) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};

		//地区三级联动
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		/***分类三级联动***/
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
		//下拉侦听
		$scope.listen_cat1 = function (data) {
			$scope.input.cat2 = {id:0,name:"请选择中分类"};
			$scope.input.cat3 = {id:0,name:"请选择小分类"};
			$scope.cat3_items = '';
		};
		$scope.listen_cat2 = function (data) {
			$scope.input.cat3 = {id:0,name:"请选择小分类"};
		};
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		//分类三级联动
		//GoCatTypeAuth.init_load_only($scope, items_cat);//改到主接口调用完毕后，再调用
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		//商品类型:二手产品为9，租赁产品为10
		$scope.getPageType = function () {
			if ($scope.selectTab == 0) {//如果为第一个tab，则是"二手"产品
				$scope.pageType = 9;
			} else {
				$scope.pageType = 10;
			}
		}

		/****************上下拉刷新**********************/
		$scope.isLoad = false;
		$scope.doRefresh = function () {
			$scope.isLoad = true;
			Unsalableused.getlist(1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function (data) {
				if (data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			}, function () {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function () {
					$scope.isLoad = false;
				}, 1500);
			});

		};
		//加载更多
		$scope.doRefreshMore = function () {
			$scope.isLoad = true;
			Unsalableused.getlist($scope.input.nPage + 1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function (data) {
				//				console.log(data)
				if (data.data.length > 0) {
					if ($scope.datas.data != undefined) {
						$scope.datas.data = $scope.datas.data.concat(data.data);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			}, function () {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function () {
					$scope.isLoad = false;
				}, 1500);
			})
		};
		/****************上下拉刷新**********************/

		$scope.get_brand = function () {
			$scope.store_cat = '';
			if ($scope.input.cat1) {
				$scope.store_cat = $scope.input.cat1.id;
			}
			if ($scope.input.cat2) {
				$scope.store_cat += ',' + $scope.input.cat2.id;
			}
			if ($scope.input.cat3) {
				$scope.store_cat += ',' + $scope.input.cat3.id;
			}
		}
		$scope.get_region = function () {
			$scope.region = '';
			if ($scope.input.province) {
				$scope.region = $scope.input.province.id;
			}
			if ($scope.input.city) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id;
			}
			if ($scope.input.district) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id + ',' + $scope.input.district.id;
			}
		}

		//查询，排序。 1：默认排序    4：价格从高到低   5：价格从低到高
		$scope.doSearch = function (orderby) {
			if (orderby == 1) {
				$scope.orderby = 1;
			}
			if (orderby > 1) {//即为4或5
				$scope.orderby = $scope.orderby == 4 ? 5 : 4;
			}

			$scope.get_brand();//获取品牌
			$scope.get_region();//获取地区
			$scope.getPageType();//是二手，还是租赁
			$scope.init_data();//初始化数据
		};

		//侧边菜单
		$scope.rightSideMenu = function () {
			$ionicSideMenuDelegate.toggleRight();
		};

		//初始化数据
		$scope.init_data = function () {
			$scope.firstHide = false;//避免闪现“没有数据”
			Unsalableused.getlist(1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function (data) {
				if (data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
				$scope.isLoad = false;
				$scope.firstHide = true;
				//分类三级联动
				GoCatTypeAuth.init_load_only($scope, items_cat);//主接口调用完毕后再调用
			})
		}
		//$scope.init_data();//初始化数据

		//页面跳转
		$scope.onshow_post = function () {
			GoPage.go_states("newproductpost");
		}
		$scope.onShow_detail = function (goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" })
		}
		$scope.onshow_first = function (store_id) {
			GoPage.go_states("first", { store_id: store_id });
		}
		$scope.clear_key = function () {
			$scope.input.key = '';
		}


	}

	controller.$inject = ['$scope', 'Unsalableused', 'GoAddress','GoDialog', '$window', '$timeout', '$ionicSideMenuDelegate', 'GoPage', 'GoCatTypeAuth', '$ionicScrollDelegate'];

	return app.registerController("unsalableusedctrl", controller);
});